package fag.editor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import javax.swing.JOptionPane;

public class Serializer {

	FileOutputStream osFile;
	FileInputStream  isFile;
	ObjectOutputStream osObject;
	ObjectInputStream isObject;
	
	public Serializer() {
		
	}
	
	public void Serialize(Serializable s,String filename) {
		
		try {
			osFile = new FileOutputStream(filename);
			osObject = new ObjectOutputStream(osFile);
			osObject.writeObject(s);
			osObject.flush();
			osObject.close();
			JOptionPane.showMessageDialog(null, "Serialisation Terminé", "Editor Popup", JOptionPane.INFORMATION_MESSAGE);
		}		
		catch (java.io.IOException e) {
			e.printStackTrace();
		}
	}
	
	public Object Deserialize(String filename) {
	
		Object toReturn = null;
		
		try {
			isFile = new FileInputStream(filename);
			isObject = new ObjectInputStream(isFile);
			toReturn = isObject.readObject();
			JOptionPane.showMessageDialog(null, "DeSerialisation Terminé", "Editor Popup", JOptionPane.INFORMATION_MESSAGE);
		}
		catch (java.io.IOException e) {
			e.printStackTrace();
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return toReturn;				
	}
}
